package com.jh3502.vivo.function.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseActivity;
import com.jh3502.vivo.function.citylist.CityBean;
import com.jh3502.vivo.function.contact.ContactBean;
import com.jh3502.vivo.function.reimnew.ReimNewPresenter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author harisucici
 * @version 2018/12/11 10:10
 * name
 * todo
 * @email harisucici@gmail.com
 */
public class TravelNumChooseDialog extends BaseActivity<ReimNewPresenter.ReimNewView, ReimNewPresenter> implements ReimNewPresenter.ReimNewView {

    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.list)
    ListView list;
    @BindView(R.id.cancel)
    TextView cancel;
    @BindView(R.id.submit)
    TextView submit;
    private String startDate;
    private String endDate;
    private String place;
    private String travelNo;
    private int travelId;

    @Override
    public ReimNewPresenter initPresenter() {
        return new ReimNewPresenter();
    }

    @Override
    protected int setViewId() {
        return R.layout.activity_travel_num_choose;
    }

    @Override
    protected void initView() {
        mPresenter.setReimNewView(this);


    }

    @Override
    protected void initData() {
        Map<String, Object> body = new HashMap<>(2);
        body.put("name", getIntent().getExtras().getString("name"));
        mPresenter.findTravelForm(body);
    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {

    }


    @Override
    public void findExpensePersonSuccess(List<TravelPersonChooseBean> travelPersonChooseBeans) {

    }

    @Override
    public void findTravelFormSuccess(List<TravelNumChooseBean> datas) {
        ListChoiceAdapter adapter = new ListChoiceAdapter(this, datas);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startDate = datas.get(position).getStartDate();
                endDate = datas.get(position).getEndDate();
                place = datas.get(position).getDestination();
                travelNo = datas.get(position).getBusinessNo();
                travelId = datas.get(position).getId();
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void findaccountSuccess(Map<String, Object> map) {

    }

    @Override
    public void submitExpenseSuccess() {

    }

    @Override
    public void findHotelStandard(double v) {

    }

    @OnClick(R.id.cancel)
    public void onCancelClicked() {
        onBackPressed();
    }

    @OnClick(R.id.submit)
    public void onSubmitClicked() {
        if (TextUtils.isEmpty(travelNo)) {
            showShortToast("最少选择一项");
            return;
        }
        Intent intent = new Intent();
        intent.putExtra("startDate", startDate);
        intent.putExtra("endDate", endDate);
        intent.putExtra("place", place);
        intent.putExtra("travelNo", travelNo);
        intent.putExtra("travelId", travelId);

        setResult(Activity.RESULT_OK, intent);
        onBackPressed();
    }

    private class ListChoiceAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private List<TravelNumChooseBean> list;

        public ListChoiceAdapter(Context context, List<TravelNumChooseBean> list) {
            this.inflater = LayoutInflater.from(context);
            this.list = list;


        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ListChoiceAdapter.ViewHolder holder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_list_num_choice, null);
                holder = new ListChoiceAdapter.ViewHolder();
                holder.num = convertView.findViewById(R.id.num);
                holder.person = convertView.findViewById(R.id.person);
                holder.place = convertView.findViewById(R.id.place);
                holder.date = convertView.findViewById(R.id.date);
                holder.check = convertView.findViewById(R.id.check);

                convertView.setTag(holder);
            } else {
                holder = (ListChoiceAdapter.ViewHolder) convertView.getTag();
            }
            boolean check = ((ListView) parent).isItemChecked(position);
            holder.check.setChecked(check);

            holder.num.setText(list.get(position).getBusinessNo());
            List<CityBean> cities = new Gson().fromJson(list.get(position).getDestination(),
                    new TypeToken<List<CityBean>>() {
                    }.getType());
            StringBuffer stringBufferCity = new StringBuffer();
            for (CityBean cityBean : cities) {
                stringBufferCity.append(cityBean.getName());
                stringBufferCity.append("、");
            }
            holder.place.setText(
                    stringBufferCity.toString().substring(0, stringBufferCity.toString().length() - 1));


            List<ContactBean> persons = new Gson().fromJson(list.get(position).getPerson(),
                    new TypeToken<List<ContactBean>>() {
                    }.getType());
            StringBuffer stringBufferPeople = new StringBuffer();
            for (ContactBean contactBean : persons) {
                stringBufferPeople.append(contactBean.getName());
                stringBufferPeople.append("、");
            }
            holder.person.setText(
                    stringBufferPeople.toString().substring(0, stringBufferPeople.toString().length() - 1));

            holder.date.setText(list.get(position).getStartDate());
            return convertView;
        }

        private class ViewHolder {
            TextView num;
            TextView person;
            TextView place;
            TextView date;
            CheckBox check;

        }
    }
}
